//
//  UIBarButton+Extension.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/9/27.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

extension UIBarButtonItem{
    /// 自定义的导航按钮
    ///
    /// - parameter imageName:   默认图片
    /// - parameter bgImageName: 背景图片
    ///
    /// - returns: 导航按钮
    convenience init(imageName: String,bgImageName: String, target:Any?, action:Selector) {
        //初始化按钮
        let btn = UIButton(type: .custom)
        //先获得图片
        let nomImage = UIImage(named: imageName)
        //获得图片尺寸
        let imgSize = nomImage?.size ?? CGSize()
        let rect = CGRect(x: 0, y: 0, width: imgSize.width, height: imgSize.height)
        btn.frame = rect
        btn.setBackgroundImage(UIImage(named: imageName), for: .normal)
        btn.setBackgroundImage(UIImage(named: bgImageName), for: .highlighted)
        //点击事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        //添加按钮到初始化方法
        self.init(customView:btn)
    }
}
